package org.lisen.controlleradvice.handler;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.util.Date;

/**
 * @author laughing
 * @date 2020/9/26
 * @site https://www.lisen.org
 */
@RestControllerAdvice
public class GlobalDataBinder {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                Date date = null;
                try {
                    if (text != null) {
                        date = DateUtils.addDays(DateUtils.parseDate(text,"yyyy-MM-dd"),1);
                    }
                } catch (ParseException e) {
                }
                setValue(date);
            }
        });
    }

}
